#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int findLHS(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int res = 0;
        for (int begin = 0, end = 0; end < nums.size(); end++)
        {
            while (end > begin && nums[end] - nums[begin] > 1)
            {
                ++begin;
            }
            if (nums[end] - nums[begin] == 1)
            {
                res = max(res, end - begin + 1);
            }
        }
        return res;
    }
};